﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.Merchants;
using VK.Comm;

namespace VK.Casemeet.Web.Front
{
    public partial class ShopRegister : System.Web.UI.Page
    {
        public string strMessage = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShopHelper.BindTypeDDL(ddlType, OptionType.Default);
            }
        }

        protected void butSubmit_Click(object sender, EventArgs e)
        {
            if (txtAccount.Text.Length == 0)
            {
                strMessage += "<p>帐号不能为空!</p>";
            }
            if (txtPass.Text.Length == 0)
            {
                strMessage += "<p>密码不能为空!</p>";
            }
            else
            {
                if (txtPass.Text.Length < 4 || txtPass.Text.Length > 20)
                {
                    strMessage += "<p>密　码(4-20)位字符!</p>";
                }
                else
                {
                    if (!string.Equals(txtPass.Text, txtPassCheck.Text))
                    {
                        strMessage += "<p>两次密码输入要相同!</p>";
                    }
                }
            }
            if (txtName.Text.Length == 0)
            {
                strMessage += "<p>企业全称不能为空!</p>";
            }
            if (ddlType.SelectedValue.ToString() == "0")
            {
                strMessage += "<p>请选择企业类型!</p>";
            }
            int provinceID = Util.GetFormInt("ddlArea1");
            if (provinceID == 0)
            {
                strMessage += "<p>请选择省!</p>";
            }
            int cityID = Util.GetFormInt("ddlArea2");
            if (cityID == 0)
            {
                strMessage += "<p>请选择市!</p>";
            }
            int areaID = Util.GetFormInt("ddlArea3");
            if (areaID == 0)
            {
                strMessage += "<p>请选择区!</p>";
            }
            if (txtContact.Text.Length == 0)
            {
                strMessage += "<p>联系人不能为空!</p>";
            }
            if (txtTelephone.Text.Length == 0)
            {
                strMessage += "<p>联系电话不能为空!</p>";
            }
            if (txtEmail.Text.Length == 0)
            {
                strMessage += "<p>电子邮箱不能为空!</p>";
            }
            else
            {
                if (!RegexStr.CheckEmail(txtEmail.Text))
                {
                    strMessage += "<p>电子邮箱只能为邮箱格式!</p>";
                }
            }
            if (txtQQ.Text.Length == 0)
            {
                strMessage += "<p>联系QQ不能为空!</p>";
            }
            else
            {
                if (!RegexStr.CheckNum(txtQQ.Text))
                {
                    strMessage += "<p>联系QQ只能为数字格式!</p>";
                }
            }
            if (txtAddress.Text.Length == 0)
            {
                strMessage += "<p>通讯地址不能为空!</p>";
            }
            if (ImgLogo.ValueUrl == "Null.jpg")
            {
                strMessage += "<p>企业Logo不能为空!</p>";
            }

            if (txtCode.Text.Trim().Length == 0)
            {
                strMessage += "<p>验证码不能为空!</p>";
            }
            if (Util.GetCookieString("AdminValidateCode") != txtCode.Text.ToLower())
            {
                strMessage += "<p>验证码输入错误!</p>";
            }

            if (!string.IsNullOrEmpty(strMessage))
            {
                strMessage = "<dd class='regMsg'>" + strMessage;
                strMessage = strMessage + "</dd>";
                return;
            }

            ShopEntity shop = LoadEntity();

            switch (AdminBLL.Register(shop))
            {
                case 1: Response.Redirect("/ShopAdmin/Home.aspx"); break;
                case 3: strMessage = "<dd id='vcodeMsg' class='regMsg'><p>请更换帐号，此帐号已有人使用!</p></dd>"; break;
            }
        }

        protected ShopEntity LoadEntity()
        {
            ShopEntity shop = new ShopEntity();
            shop.Account = txtAccount.Text.Trim();
            shop.Password = DES.Encrypt(txtPass.Text.Trim(), CS.ShopAdminPassKey);
            shop.Type = int.Parse(ddlType.SelectedValue);
            shop.Name = txtName.Text.Trim();
            shop.Level = 1;
            shop.Telephone = txtTelephone.Text.Trim();
            shop.Capital = 0;
            shop.ProvinceID = Util.GetFormInt("ddlArea1");
            shop.CityID = Util.GetFormInt("ddlArea2");
            shop.AreaID = Util.GetFormInt("ddlArea3");
            shop.Address = txtAddress.Text.Trim();
            shop.Map = Util.GetFormString("txtMap");
            shop.Hits = 0;
            shop.IsCertification = false;
            shop.State = 3;
            shop.Notice = string.Empty;
            shop.LicenceUrl = string.Empty;
            shop.Email = txtEmail.Text.Trim();
            shop.Company = txtCompany.Text.Trim();
            shop.Contact = txtContact.Text.Trim();
            shop.LogoUrl = ImgLogo.ValueUrl;
            shop.Map = CS.ShopMap;
            shop.Hits = 1;
            shop.IsCertification = false;
            shop.State = 1;

            return shop;
        }
    }
}